﻿#region Using Statements
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
#endregion

namespace SoundManager
{
    class SoundManager : ISoundManager
    {
        public static readonly string FIRE = "shotgun_fire";
        public static readonly string LOAD = "shotgun_load";

        // Data members
        protected static ISoundManager pInstance;

        protected static AudioEngine audioEngine;
        protected static WaveBank waveBank;
        protected static SoundBank soundBank;

        protected static AudioEmitter audioEmitter;
        private AudioListener listener;


        /// <summary>
        /// Default constructor for the Sound Manager
        /// </summary>

        public SoundManager()
        {
            audioEngine = new AudioEngine("Content/sounds/sound.xgs");
            waveBank = new WaveBank(audioEngine, "Content/sounds/Wave Bank.xwb");
            soundBank = new SoundBank(audioEngine, "Content/sounds/Sound Bank.xsb");

            listener = new AudioListener();
            audioEmitter = new AudioEmitter();

        }

        /// <summary>
        /// Set the listener position (should be our camera)
        /// </summary>
        /// <param name="listener">The Vector3 that represents the camera's position</param>

        public void setListenerPosition(Vector3 listener)
        {
            this.listener.Position = listener;
        }

        /// <summary>
        /// Update the audio engine
        /// </summary>

        public void updateEngine()
        {
            SoundManager.audioEngine.Update();
        }

        /// <summary>
        /// Play a sound
        /// </summary>
        /// <param name="emitterPosition">The Vector3 that represents the emitter's position</param>
        /// <param name="soundName">The string that represents the name of the sound to play</param>

        public void playSound(Vector3 emitterPosition, String soundName)
        {
            SoundManager.audioEmitter.Position = emitterPosition;
            Cue cue = soundBank.GetCue(soundName);
            cue.Apply3D(this.listener, SoundManager.audioEmitter);
            cue.Play();
        }

        public void playBackgroundMusic()
        {
        }


        /// <summary>
        /// Returns an instance of the SoundManager class, initializing it if necessary
        /// </summary>
        /// <returns>Returns an instance of the SoundManager class</returns>

        public static ISoundManager GetInstance()
        {
            if (pInstance == null)
            {
                pInstance = new SoundManager();
            }

            return pInstance;
        }

    }
}
